#include <stdlib.h>

#include "arraylist.h"
#include "arrayqueue.h"

#define INITIAL_STACK_CAP		10

/*
 * Structure declarations
 */

struct arrayqueue_adt {
	ArrayList list;
	int count;
};

/*
 * Public functions
 */

ArrayQueue AQ_New() {
	ArrayQueue result;
	
	result = (ArrayQueue) malloc(sizeof(struct arrayqueue_adt));
	
	result->list = AL_New(INITIAL_STACK_CAP);
	result->count = 0;
	
	return result;
}

void AQ_Free(ArrayQueue queue) {
	if (queue == NULL)
		return;
	
	AL_Free(queue->list);
	
	free(queue);
}

int AQ_Size(ArrayQueue queue) {
	if (queue == NULL)
		return -1;
	
	return queue->count;
}

int AQ_Empty(ArrayQueue queue) {
	if (queue == NULL)
		return -1;
	
	return (queue->count == 0) ? 1 : 0;
}

void AQ_Enqueue(ArrayQueue queue, void *el) {
	if (queue == NULL)
		return;
	
	AL_Add(queue->list, AL_End(queue->list), el);
	
	queue->count++;
}

void AQ_Dequeue(ArrayQueue queue) {
	if (queue == NULL || queue->count == 0)
		return;
	
	AL_Remove(queue->list, AL_Begin(queue->list));
	
	queue->count--;
}

void *AQ_Front(ArrayQueue queue) {
	if (queue == NULL || queue->count == 0)
		return NULL;
	
	return AL_Get(queue->list, AL_Begin(queue->list));
}
